package cn.lg.soar.common.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2023/11/20 18:44
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public interface IFormatter<T> {

    /**
     * 格式化
     * @param value
     * @return
     */
    String format(T value);

    /**
     * 格式化
     * @param value
     * @param def 默认值
     * @return
     */
    String format(T value, String def);


    /**
     * 解析
     * @param source
     * @return
     */
    T parse(String source);

    /**
     * 解析
     * @param source
     * @param def 默认值
     * @return
     */
    T parse(String source, T def);


    /**
     * 日期时间格式器
     */
    public class LocalDateTimeFormatter implements IFormatter<LocalDateTime> {

        private final DateTimeFormatter formatter;

        public LocalDateTimeFormatter(DateTimeFormatter formatter) {
            this.formatter = formatter;
        }

        public LocalDateTimeFormatter(String pattern) {
            this.formatter = DateTimeFormatter.ofPattern(pattern);
        }

        @Override
        public String format(LocalDateTime value) {
            if (value == null) {
                return null;
            }
            return formatter.format(value);
        }

        @Override
        public String format(LocalDateTime value, String def) {
            if (value == null) {
                return def;
            }
            return formatter.format(value);
        }

        @Override
        public LocalDateTime parse(String source) {
            if (source == null) {
                return null;
            }
            return LocalDateTime.parse(source, formatter);
        }

        @Override
        public LocalDateTime parse(String source, LocalDateTime def) {
            if (source == null) {
                return def;
            }
            return LocalDateTime.parse(source, formatter);
        }

    }

    /**
     * 日期格式器
     */
    public class LocalDateFormatter implements IFormatter<LocalDate> {

        private final DateTimeFormatter formatter;

        public LocalDateFormatter(DateTimeFormatter formatter) {
            this.formatter = formatter;
        }
        public LocalDateFormatter(String pattern) {
            this.formatter = DateTimeFormatter.ofPattern(pattern);
        }

        @Override
        public String format(LocalDate value) {
            if (value == null) {
                return null;
            }
            return formatter.format(value);
        }

        @Override
        public String format(LocalDate value, String def) {
            if (value == null) {
                return def;
            }
            return formatter.format(value);
        }

        @Override
        public LocalDate parse(String source) {
            if (source == null) {
                return null;
            }
            return LocalDate.parse(source, formatter);
        }

        @Override
        public LocalDate parse(String source, LocalDate def) {
            if (source == null) {
                return def;
            }
            return LocalDate.parse(source, formatter);
        }

    }

    /**
     * 时间格式器
     */
    public class LocalTimeFormatter implements IFormatter<LocalTime> {

        private final DateTimeFormatter formatter;

        public LocalTimeFormatter(DateTimeFormatter formatter) {
            this.formatter = formatter;
        }
        public LocalTimeFormatter(String pattern) {
            this.formatter = DateTimeFormatter.ofPattern(pattern);
        }

        @Override
        public String format(LocalTime value) {
            if (value == null) {
                return null;
            }
            return formatter.format(value);
        }

        @Override
        public String format(LocalTime value, String def) {
            if (value == null) {
                return def;
            }
            return formatter.format(value);
        }

        @Override
        public LocalTime parse(String source) {
            if (source == null) {
                return null;
            }
            return LocalTime.parse(source, formatter);
        }

        @Override
        public LocalTime parse(String source, LocalTime def) {
            if (source == null) {
                return def;
            }
            return LocalTime.parse(source, formatter);
        }

    }

    /**
     * 时间格式器
     */
    public class DateFormatter implements IFormatter<Date> {

        private final String pattern;

        public DateFormatter(String pattern) {
            this.pattern = pattern;
        }

        @Override
        public String format(Date value) {
            if (value == null) {
                return null;
            }
            return new SimpleDateFormat(pattern).format(value);
        }

        @Override
        public String format(Date value, String def) {
            if (value == null) {
                return def;
            }
            return new SimpleDateFormat(pattern).format(value);
        }

        @Override
        public Date parse(String source) {
            if (source == null) {
                return null;
            }
            try {
                return new SimpleDateFormat(pattern).parse(source);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public Date parse(String source, Date def) {
            if (source == null) {
                return def;
            }
            try {
                return new SimpleDateFormat(pattern).parse(source);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

    }

    /**
     * 数值格式器
     */
    public class NumberFormatter implements IFormatter<Number> {

        private final DecimalFormat decimalFormat;

        public NumberFormatter(String pattern) {
            this.decimalFormat = new DecimalFormat(pattern);
        }

        public NumberFormatter(DecimalFormat decimalFormat) {
            this.decimalFormat = decimalFormat;
        }

        @Override
        public String format(Number value) {
            if (value == null) {
                return null;
            }
            return decimalFormat.format(value);
        }

        @Override
        public String format(Number value, String def) {
            if (value == null) {
                return def;
            }
            return decimalFormat.format(value);
        }

        @Override
        public Number parse(String source) {
            if (source == null) {
                return null;
            }
            try {
                return decimalFormat.parse(source);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public Number parse(String source, Number def) {
            if (source == null) {
                return def;
            }
            try {
                return decimalFormat.parse(source);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

    }

}
